import {
	requestPayment
} from '@/utils/app'
export default {
	data() {
		return {
			detail: {},
			orderId: ''
		};
	},
	onLoad(options) {
		this.orderId = options.orderId
		this.getOrderDetail()
	},
	methods: {
		// 获取订单详情
		async getOrderDetail() {
			var res = await this.$get('server/index/getorderdetails', {
				id: this.orderId,
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			this.detail = res.order
		},
		// 复制订单号
		onCopy() {
			uni.setClipboardData({
				data: this.detail.orderid, //要被复制的内容
				success: () => { //复制成功的回调函数
					this.$toast('复制成功')
				}
			});
		},
		// 确认收货
		onShouhuo() {
			this.$showModal({
				title: '提示',
				content: '是否确认收货？',
				success: (res) => {
					if (res.confirm) {
						console.log('用户点击确定')
						this.shouhuoApi()
					} else if (res.cancel) {
						console.log('用户点击取消')
					}
				}
			})
		},
		async shouhuoApi() {
			var res = await this.$get('server/index/shouhuo', {
				id: this.detail.id,
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			this.$toast('确认收货成功')
			setTimeout(() => {
				uni.navigateBack()
				uni.$emit('updateOrder')
			}, 500)
		},
		// 去支付
		async onPay() {
			var res = await this.$get('server/index/topay', {
				id: this.detail.id,
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}

			requestPayment(res.res, 1)
				.then(() => {
					this.$toast('支付成功')
					setTimeout(() => {
						uni.navigateBack()
						uni.$emit('updateOrder')
					}, 500)
				})
				.catch(err => this.$toast('订单未支付'))
				.finally(() => {
					this.$toast('取消支付')
				})
		},
		// 取消订单
		onCancel() {
			this.$showModal({
				title: '提示',
				content: '确认取消订单？',
				success: (res) => {
					if (res.confirm) {
						console.log('用户点击确定')
						this.cancelApi()
					} else if (res.cancel) {
						console.log('用户点击取消')
					}
				}
			})
		},
		async cancelApi() {
			var res = await this.$get('server/index/cancelorder', {
				id: this.orderId,
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			this.$toast('取消成功')
			setTimeout(() => {
				uni.navigateBack()
				uni.$emit('updateOrder')
			}, 500)
		},

	}
};
